<?php
/**
 * ApiBaseController.php.
 *
 * @author: cnx7 <zysafe@live.cn> 2019-07-25
 */

namespace common\domain\base;

use common\domain\components\filters\ApiAuth;
use common\domain\components\filters\RequestId;
use common\domain\components\filters\SignFilter;
use yii\filters\ContentNegotiator;
use yii\filters\Cors;
use yii\filters\VerbFilter;
use yii\rest\Controller;
use yii\web\Response;

class ApiBaseController extends Controller
{
    use ApiResponse;

    public function behaviors(): array
    {
        $behaviors = [
            'corsFilter' => [
                'class' => Cors::className(),
            ],
            ['class' => SignFilter::className()],
            ['class' => RequestId::className()],
            'contentNegotiator' => [
                'class' => ContentNegotiator::className(),
                'formats' => [
                    'application/json' => Response::FORMAT_JSON,
                ],
            ],
            'verbFilter' => [
                'class' => VerbFilter::className(),
                'actions' => $this->verbs(),
            ],
            'authenticator' => [
                'class' => ApiAuth::className(),
            ],
        ];

        if (defined('YII_ENV') && YII_ENV === 'dev') {
            foreach ($behaviors as $key => $value) {
                if ($value['class'] === SignFilter::className()) {
                    unset($behaviors[$key]);
                    break;
                }
            }
        }

        return $behaviors;
    }
}
